package com.to.aboomy.bannersample.pagetransformer;

import com.to.aboomy.banner.BannerViewPager;
import ohos.agp.components.Component;

/**
 * @author xujianhong
 * @since 2021-08-05
 */
public class DepthPageTransformer implements BannerViewPager.PageTransformer{

    @Override
    public void transformerPage(BannerViewPager.PageType pageType, Component page, float offset, float offsetPixels) {

        switch (pageType) {
            case PREVIOUS:
                page.setTranslationX(0.0F);
                page.setScaleX(1.0F);
                page.setScaleY(1.0F);
                page.setAlpha(1f);
                break;
            case CURRENT:
                if (offset <= 0.0F) {
                    page.setTranslationX(0.0F);
                    page.setScaleX(1.0F);
                    page.setScaleY(1.0F);
                    page.setAlpha(1f);
                } else if (offset <= 1.0F) {
                    float scaleFactor = 0.75F + 0.25F * ((float)1 - Math.abs(offset));
                    page.setAlpha((float)1 - offset);
                    page.setPivotY(0.5F * (float)page.getHeight());
                    page.setTranslationX((float)page.getWidth() * -offset);
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                }
                break;

            case NEXT:
                offset = 1 - offset;
                if (offset <= 0.0F) {
                    page.setTranslationX(0.0F);
                    page.setScaleX(1.0F);
                    page.setScaleY(1.0F);
                    page.setAlpha(1f);
                } else if (offsetPixels >0) {
                    float scaleFactor = 0.75F + 0.25F * ((float)1 - Math.abs(offset));
                    page.setAlpha((float)1 - offset);
                    page.setPivotY(0.5F * (float)page.getHeight());
                    page.setTranslationX((float)page.getWidth() * -offset);
                    page.setScaleX(scaleFactor);
                    page.setScaleY(scaleFactor);
                }
                break;

            case OTHER:
                page.setTranslationX(0.0F);
                page.setScaleX(1.0F);
                page.setScaleY(1.0F);
                page.setAlpha(1f);
                break;
        }
    }
}
